package com.medusa.aps.business.modules.basic.controller;

import com.medusa.aps.business.model.pagination.Page;
import com.medusa.aps.business.modules.basic.model.input.WorkshopDTO;
import com.medusa.aps.business.modules.basic.model.input.WorkshopParam;
import com.medusa.aps.business.modules.basic.mp.entity.Workshop;
import com.medusa.aps.business.modules.basic.mp.service.WorkshopService;
import com.medusa.aps.business.common.model.resp.Result;
import com.medusa.aps.business.common.web.valid.group.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RestController
@Tag(name = "车间按控制层")
@RequestMapping("/workshop")
public class WorkshopController {

    @Resource
    private WorkshopService workshopService;

    /**
     * 新增车间
     *
     * @param workshops 车间列表
     */
    @PostMapping("/save")
    @Operation(description = "新增车间", method = "POST", summary = "新增车间")
    public Result<Void> saveBatchWorkshop(@RequestBody @Valid @Size(min = 1, max = 50, message = "车间长度最小为1,最长为50") List<WorkshopDTO> workshops) {
        workshopService.saveBatchWorkshop(workshops);
        return Result.ok();
    }

    /**
     * 编辑车间
     *
     * @param workshopDTO 车间DTO
     */
    @PutMapping("/update")
    @Operation(description = "编辑车间", method = "PUT", summary = "编辑车间")
    public Result<Void> updateWorkshop(@RequestBody @Validated(Update.class) WorkshopDTO workshopDTO) {
        workshopService.updateWorkshop(workshopDTO);
        return Result.ok();
    }

    /**
     * 查询车间
     *
     * @param id 车间id
     */
    @GetMapping("/getOne")
    @Operation(description = "查询车间", method = "GET", summary = "查询车间")
    public Result<Workshop> getWorkshop(@RequestParam("id") Integer id) {
        return Result.ok(
                workshopService.getById(id)
        );
    }

    /**
     * 分页查询车间
     *
     * @param workshopParam 车间查询参数
     */
    @GetMapping("/list")
    @Operation(description = "分页查询车间", method = "GET", summary = "分页查询车间")
    public Result<Page<Workshop>> pageWorkshop(WorkshopParam workshopParam) {
        return Result.ok(
                workshopService.pageWorkshop(workshopParam)
        );
    }


}
